Create and copy a file

The following example creates a file for text writing and reading.

C# .NET

public static String DoTest()
{
         string strRet = "";
         // Specify the files you want to manipulate.
         string path = @"c:\MyFile.txt";
         string path2 = path + "temp";
         try
         {
                  // Determine whether the file exists.
                  if (!File.Exists(path))
                  {
                           // Create the file; it does not exist.
                           StreamWriter sw = File.CreateText(path);
                           sw.WriteLine("Hello");
                           sw.WriteLine("And");
                           sw.WriteLine("Welcome");
                           sw.Close();
                           strRet += "The source file " + path + " created successfully\r\n";
                  }
                  // Only do the Copy operation if the first file exists
                  // and the second file does not.
                  if (File.Exists(path))
                  {
                           if (File.Exists(path2))
                           {
                                    strRet += String.Format("The target already exists");
                           }
                           else
                           {
                                    // Try to copy the file.
                                    File.Copy(path, path2);
                                    strRet += String.Format("{0} was copied to {1}.", path, path2);
                           }
                  }
                  else
                  {
                           strRet += ("The source file does not exist." + Environment.NewLine);
                  }
         }
         catch (Exception e)
         {
                  strRet += String.Format("The process failed: {0}", e.ToString());
         }
         return strRet;         
}

 

Blaze++ .NET

static String DoTest()
{
         String strRet = "";
         // Specify the files you want to manipulate.
         String path = "c:\\MyFile.txt";
         String path2 = path + "temp";
         try
         {
                  // Determine whether the file exists.
                  if (!File::Exists(path))
                  {
                           // Create the file; it does not exist.
                           StreamWriter sw = File::CreateText(path);
                           sw.WriteLine("Hello");
                           sw.WriteLine("And");
                           sw.WriteLine("Welcome");
                           sw.Close();
                           strRet += "The source file " + path + " created successfully\r\n";
                  }
                  // Only do the Copy operation if the first file exists
                  // and the second file does not.
                  if (File::Exists(path))
                  {
                           if (File::Exists(path2))
                           {
                                    strRet += String::Format("The target already exists");
                           }
                           else
                           {
                                    // Try to copy the file.
                                    File::Copy(path, path2);
                                    strRet += String::Format("{0} was copied to {1}.", path, path2);
                           }
                  }
                  else
                  {
                           strRet += ("The source file does not exist." + Environment::NewLine);
                  }
         }
         catch (Exception e)
         {
                  strRet += String::Format("The process failed: {0}", e.ToString());
         }
         return strRet;         
}